package com.siwei.mes.job.laborator;

import com.siwei.mes.common.base.BaseParams;
import com.siwei.mes.common.util.MacTools;
import com.siwei.mes.entity.DownloadLaboratoryEntity;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.enums.CheckEnum;
import com.siwei.mes.enums.ExperimentStatusEnum;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.enums.SystemParamEnum;
import com.siwei.mes.mapper.experiment.ExperimentMapper;
import com.siwei.mes.service.system.SystemParamService;
import com.siwei.mes.sync.config.SyncProcessorService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 定时下载原材料试验数据
 **/
@Component
public class RawMaterialDownloadXhJob {
    private static final Logger log = LoggerFactory.getLogger("job");

    @Resource
    private ExperimentMapper experimentMapper;
    @Resource
    private SystemParamService systemParamService;
    @Resource
    private SyncProcessorService syncProcessorService;


    @Scheduled(fixedRate = 3600000)
    //@Scheduled(cron = "0 0/5 * * * ?")
    public void init() {
        String operSystem = MacTools.getOperSystem();
        if (Arrays.stream(BaseParams.MAC_LIST).parallel().anyMatch(operSystem::contains)) {
            return;
        }
        log.info("拉取协会原材料数据到品控定时任务开始！");
        String systemParamValue = systemParamService.selectBySystemCode(SystemParamEnum.PULL_MATERIAL_XH_DATA_TO_MES_JOB, "0");
        if (StringUtils.isBlank(systemParamValue) || "0".equals(systemParamValue)) {
            log.info("系统参数：【{}】拉取协会原材料数据到品控定时任务，暂未开启推，不拉取协会数据！", SystemParamEnum.PULL_MATERIAL_XH_DATA_TO_MES_JOB);
            return;
        }

        try {

            //默认查询30天的 原材料 试验中的数据
            Date currentDate = new Date();
            Date startDate = new Date(currentDate.getTime() - 30L * 24 * 60 * 60 * 1000);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String beginTime = sdf.format(startDate);
            String comTime = sdf.format(currentDate);
            /*.setBeginTime(beginTime).setComTime(comTime)*/


            List<Integer> experimentTypeList = new ArrayList<>();
            experimentTypeList.add(MaterialsEnum.CEMENT.getCode());
            experimentTypeList.add(MaterialsEnum.FLY.getCode());
            experimentTypeList.add(MaterialsEnum.SLAG.getCode());
            experimentTypeList.add(MaterialsEnum.COARSE.getCode());
            experimentTypeList.add(MaterialsEnum.FINE.getCode());
            experimentTypeList.add(MaterialsEnum.ADMIXTURE.getCode());

            Experiment exp = new Experiment().setExperimentTypeList(experimentTypeList).setCheckType(CheckEnum.BATCH.getCode()).setExperimentStatus(ExperimentStatusEnum.IN_TEST.getCode());
            List<Experiment> experimentList = experimentMapper.getList(exp);
            if (experimentList.isEmpty()) {
                log.info("自动下载原材料协会数据,查询最近30天的 原材料 试验中的数据 为空！等待下一次执行");
                return;
            }

            for (Experiment experiment : experimentList) {
                //协会委托为空 则不处理
                if (StringUtils.isBlank(experiment.getConsignId())) {
                    log.info("自动下载原材料协会数据,委托id：【{}】，委托编号：【{}】协会委托为空 则不处理！", experiment.getId(), experiment.getExperimentNo());
                    continue;
                }
                /**这里改成一个一个提交 避免一次提交太多数据 导致超时 */
                DownloadLaboratoryEntity downloadLaboratoryEntity = new DownloadLaboratoryEntity()
                        .setExperimentId(Collections.singletonList(String.valueOf(experiment.getId())));
                syncProcessorService.submitRequest(downloadLaboratoryEntity);
            }
            log.info("自动下载原材料协会数据结束!");
        } catch (Exception e) {
            log.error("定时下载协会原材料试验数据异常：" + e.getMessage(), e);
        }
    }
}
